<?php
session_start();
error_reporting(0); //hendel error
//Deteksi hanya bisa diinclude, tidak bisa langsung dibuka langsung (direct open)
if(count(get_included_files())==4)
{
	echo "<meta http-equiv='refresh' content='0; url=http://$_SERVER[HTTP_HOST]'>";
	exit("Direct access not permitted.");
}
if (empty($_SESSION['username']) AND empty($_SESSION['passuser'])){
	echo "<meta http-equiv='refresh' content='0; url=http://$_SERVER[HTTP_HOST]'>";
	exit("Direct access not permitted.");
}else{
include "../../../config/koneksi.php";
include "../../../config/library.php";
include "../../../config/fungsi_thumb.php";
include "../../../config/fungsi_seo.php";

$module=$_GET['module'];
$act=$_GET['act'];

// Delete berita  
if ($module=='berita' AND $act=='hapus'){
//Memasukan data pada table arsip_berita
    mysql_query("INSERT INTO arsip_berita(judul,
                                    judul_seo,
                                    id_kategori,
                                    headline,
                                    username,
                                    isi_berita,
                                    jam,
                                    tanggal,
                                    hari,
                                    tag, 
                                    gambar)
							select judul,
                                    judul_seo,
                                    id_kategori,
                                    headline,
                                    username,
                                    isi_berita,
                                    jam,
                                    tanggal,
                                    hari,
                                    tag, 
                                    gambar
						from berita where id_berita='".$val->validasi($_GET['id'],'sql')."'");
	//Fungsi copy file gambar
	$datac = mysql_query("SELECT * FROM berita WHERE id_berita='".$val->validasi($_GET['id'],'sql')."'");
    $r=mysql_fetch_array($datac);
	$file1 = "../../../foto_berita/$r[gambar]";
	$file2 = "../../../foto_berita/medium_$r[gambar]";
	$file3 = "../../../foto_berita/small_$r[gambar]";
	$new_file1 = "../../../images/arsip_berita/$r[gambar]";
	$new_file2 = "../../../images/arsip_berita/medium_$r[gambar]";
	$new_file3 = "../../../images/arsip_berita/small_$r[gambar]";
	copy($file1, $new_file1);
	copy($file2, $new_file2);
	copy($file3, $new_file3);
	
      $data = mysql_query("SELECT * FROM berita WHERE id_berita='".$val->validasi($_GET['id'],'sql')."'");
      $r=mysql_fetch_array($data);
      $gambar1 = "../../../foto_berita/$r[gambar]";
      $gambar2 = "../../../foto_berita/medium_$r[gambar]";
      $gambar3 = "../../../foto_berita/small_$r[gambar]";
      if (file_exists($gambar1) AND $r[gambar] != 'default.jpg'){
          unlink($gambar1);
          unlink($gambar2);
          unlink($gambar3); 
      }
      mysql_query("DELETE FROM berita WHERE id_berita='".$val->validasi($_GET['id'],'sql')."'");
      header('location:../../media.php?module='.$module);
            
  }

// Input berita
elseif ($module=='berita' AND $act=='input'){
  $lokasi_file    = $_FILES['fupload']['tmp_name'];
  $tipe_file      = $_FILES['fupload']['type'];
  $nama_file      = $_FILES['fupload']['name'];
  $acak           = rand(1,99);
  $nama_file_unik = $acak.$nama_file; 
 
  $judul = htmlentities($_POST['judul']);
  $kategori = htmlentities($_POST['kategori']);
  
  if (!empty($_POST['tag_seo'])){
    $tag_seo = $_POST['tag_seo'];
    $tag=implode(',',$tag_seo);
  }            
  $judul_seo      = seo_title($judul);

    if(empty($judul)){
		echo "<script>window.alert('Input Berita gagal | Form Judul harus diisi');
        window.location=('../../media.php?module=berita&act=tambahberita')</script>";
        }else{
  if ($kategori == 0) {
     echo "<script>window.alert('Input gagal | Kategori harus dipilih');
        window.location=('../../media.php?module=berita&act=tambahberita')</script>";
  } else {
  // Apabila ada gambar yang diupload
  if (!empty($lokasi_file)){
    if ($tipe_file != "image/jpeg" AND $tipe_file != "image/pjpeg"){
    echo "<script>window.alert('Upload Gagal, Pastikan File yang di Upload bertipe *.JPG');
        window.location=('../../media.php?module=berita)</script>";
    }
    else{
    UploadImage($nama_file_unik);

    mysql_query("INSERT INTO berita(judul,
                                    judul_seo,
                                    id_kategori,
                                    headline,
                                    username,
                                    isi_berita,
                                    jam,
                                    tanggal,
                                    hari,
                                    tag, 
                                    gambar) 
                            VALUES('$_POST[judul]',
                                   '$judul_seo',
                                   '$_POST[kategori]',
                                   '$_POST[headline]', 
                                   '$_SESSION[namauser]',
                                   '$_POST[isi_berita]',
                                   '$jam_sekarang',
                                   '$tgl_sekarang',
                                   '$hari_ini',
                                   '$tag',
                                   '$nama_file_unik')");
  header('location:../../media.php?module='.$module);
  }
  }
  else{
    mysql_query("INSERT INTO berita(judul,
                                    judul_seo, 
                                    id_kategori,
                                    headline,
                                    username,
                                    isi_berita,
                                    jam,
                                    tanggal,
                                    tag, 
									gambar,
                                    hari) 
                            VALUES('$_POST[judul]',
                                   '$judul_seo',
                                   '$_POST[kategori]',
                                   '$_POST[headline]', 
                                   '$_SESSION[namauser]',
                                   '$_POST[isi_berita]',
                                   '$jam_sekarang',
                                   '$tgl_sekarang',
                                   '$tag',
                                   'default.jpg',
                                   '$hari_ini')");
  header('location:../../media.php?module='.$module);
  }
  
  $jml=count($tag_seo);
  for($i=0;$i<$jml;$i++){
    mysql_query("UPDATE tag SET count=count+1 WHERE tag_seo='$tag_seo[$i]'");
  }
}
}
}
// Update berita
elseif ($module=='berita' AND $act=='update'){
  $lokasi_file    = $_FILES['fupload']['tmp_name'];
  $tipe_file      = $_FILES['fupload']['type'];
  $nama_file      = $_FILES['fupload']['name'];
  $acak           = rand(1,99);
  $nama_file_unik = $acak.$nama_file; 

  if (!empty($_POST['tag_seo'])){
    $tag_seo = $_POST['tag_seo'];
    $tag=implode(',',$tag_seo);
  }

  $judul_seo = seo_title($_POST['judul']);

  // Apabila gambar tidak diganti
  if (empty($lokasi_file)){
    mysql_query("UPDATE berita SET judul       = '$_POST[judul]',
                                   judul_seo   = '$judul_seo', 
                                   id_kategori = '$_POST[kategori]',
                                   headline    = '$_POST[headline]',
                                   tag         = '$tag',
                                   isi_berita  = '$_POST[isi_berita]'  
                             WHERE id_berita   = '$_POST[id]'");
  header('location:../../media.php?module='.$module);
  }
  else{
    if ($tipe_file != "image/jpeg" AND $tipe_file != "image/pjpeg"){
    echo "<script>window.alert('Upload Gagal, Pastikan File yang di Upload bertipe *.JPG');
        window.location=('../../media.php?module=berita')</script>";
    }
    else{
    UploadImage($nama_file_unik);
    mysql_query("UPDATE berita SET judul       = '$_POST[judul]',
                                   judul_seo   = '$judul_seo', 
                                   id_kategori = '$_POST[kategori]',
                                   headline    = '$_POST[headline]',
                                   tag         = '$tag',
                                   isi_berita  = '$_POST[isi_berita]',
                                   gambar      = '$nama_file_unik'   
                             WHERE id_berita   = '$_POST[id]'");
   header('location:../../media.php?module='.$module);
   }
  }
}
// Hapus berita terpilih
elseif ($module=='berita' AND $act=='alldel'){
if(isset($_POST['submit'])) {
	$id_array = $_POST['data']; // return array
	$id_count = count($_POST['data']); // count array
	
	for($i=0; $i < $id_count; $i++) {
	$id = $id_array[$i];
    mysql_query("INSERT INTO arsip_berita(judul,
                                    judul_seo,
                                    id_kategori,
                                    headline,
                                    username,
                                    isi_berita,
                                    jam,
                                    tanggal,
                                    hari,
                                    tag, 
                                    gambar)
							select judul,
                                    judul_seo,
                                    id_kategori,
                                    headline,
                                    username,
                                    isi_berita,
                                    jam,
                                    tanggal,
                                    hari,
                                    tag, 
                                    gambar
						from berita where id_berita='$id'");
	//Fungsi copy file gambar
	$datac = mysql_query("SELECT * FROM berita WHERE id_berita='$id'");
    $r=mysql_fetch_array($datac);
	$file1 = "../../../foto_berita/$r[gambar]";
	$file2 = "../../../foto_berita/medium_$r[gambar]";
	$file3 = "../../../foto_berita/small_$r[gambar]";
	$new_file1 = "../../../images/arsip_berita/$r[gambar]";
	$new_file2 = "../../../images/arsip_berita/medium_$r[gambar]";
	$new_file3 = "../../../images/arsip_berita/small_$r[gambar]";
	copy($file1, $new_file1);
	copy($file2, $new_file2);
	copy($file3, $new_file3);
  $data=mysql_fetch_array(mysql_query("SELECT gambar FROM berita WHERE id_berita='$id'"));
   if ($data[gambar]!=''){       
     mysql_query("DELETE FROM berita WHERE id_berita='$id'");
     unlink("../../../foto_berita/$data[gambar]");   
     unlink("../../../foto_berita/small_$data[gambar]");   
	 unlink("../../../foto_berita/medium_$data[gambar]");  
      }else{ 
	mysql_query("DELETE FROM berita WHERE id_berita='$id'");
	}
	header('location:../../media.php?module=berita');
}
	}
		}

}
?>